package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.domain.CldAttendance;
import com.cleaningcloud.cld.domain.CldPayConfirm;
import com.cleaningcloud.cld.domain.CldProjectSet;
import com.cleaningcloud.cld.domain.CldReplace;
import com.cleaningcloud.cld.domain.dto.CldAttendanceDto;
import com.cleaningcloud.cld.domain.dto.CldReplaceDto;
import com.cleaningcloud.cld.domain.vo.CldAttendanceVo;
import com.cleaningcloud.cld.domain.vo.CldReplaceVo;
import com.cleaningcloud.cld.mapper.CldProjectSetMapper;
import com.cleaningcloud.cld.mapper.CldReplaceMapper;
import com.cleaningcloud.cld.service.ICldAttendanceService;
import com.cleaningcloud.cld.service.ICldPayConfirmService;
import com.cleaningcloud.cld.service.ICldReplaceService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 补卡管理Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-11-08
 */
@Service
public class CldReplaceServiceImpl implements ICldReplaceService {
    @Autowired
    private CldReplaceMapper cldReplaceMapper;
    @Autowired
    private CldProjectSetMapper cldProjectSetMapper;

    @Autowired
    private ICldAttendanceService cldAttendanceService;
    @Autowired
    private ICldAttendanceService attendanceService;
    @Autowired
    private ICldPayConfirmService cldPayConfirmService;

    /**
     * 查询补卡管理
     *
     * @param replaceId 补卡管理主键
     * @return 补卡管理
     */
    @Override
    public CldReplaceVo selectCldReplaceByReplaceId(Long replaceId) {
        return cldReplaceMapper.selectCldReplaceByReplaceId(replaceId);
    }

    /**
     * 查询补卡管理列表
     *
     * @param cldReplace 补卡管理
     * @return 补卡管理
     */
    @Override
    public List<CldReplaceVo> selectCldReplaceList(CldReplaceDto cldReplace) {
        return cldReplaceMapper.selectCldReplaceList(cldReplace);
    }

    /**
     * 新增补卡管理
     *
     * @param cldReplace 补卡管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldReplace(CldReplaceDto cldReplace) {
        if(cldReplace.getReplaceTime() == null){
            throw new RuntimeException("补卡时间不能为空！");
        }
        List<CldReplace> replaceList = new ArrayList<>();
        cldReplace.setCreateTime(DateUtils.getNowDate());
        cldReplace.setCreateId(SecurityUtils.getUserId());
        cldReplace.setCreateName(SecurityUtils.getUsername());
        cldReplace.setFlag(0L);
        cldReplace.setApproveStatus(1L);
        if (cldReplace.getStaffList() != null && !cldReplace.getStaffList().isEmpty()) {
            for (CldStaff cldStaff : cldReplace.getStaffList()) {
                CldReplace replace = new CldReplace();
                BeanUtils.copyProperties(cldReplace, replace);
                replace.setStaffId(cldStaff.getStaffId());
                isRepeatCardIssue(replace, cldStaff.getStaffName());
                replaceList.add(replace);
            }
        } else {
            isRepeatCardIssue(cldReplace, null);
            replaceList.add(cldReplace);
        }
        cldReplaceMapper.batchInsertCldReplace(replaceList);
        return 1;
    }


    public void isRepeatCardIssue(CldReplace cldReplace, String staffName) {
        // 查询考勤信息
        CldAttendanceDto cldAttendanceDto = new CldAttendanceDto();
        cldAttendanceDto.setStaffId(cldReplace.getStaffId());
        cldAttendanceDto.setAttendanceDate(cldReplace.getReplaceTime());
        List<CldAttendanceVo> attList = cldAttendanceService.selectCldAttendanceList(cldAttendanceDto);
        if (attList.isEmpty()) {
            throw new RuntimeException("当天未存在考勤数据,无需提交补卡申请！");
        }
        // 查询项目配置 获取当月最多补卡次数
        CldProjectSet cldProjectSet = cldProjectSetMapper.selectCldProjectSetByProjectId(cldReplace.getProjectId());
        // 查询员工当月补卡次数
        CldReplaceDto replaceQuery = new CldReplaceDto();
        replaceQuery.setProjectId(cldReplace.getProjectId());
        replaceQuery.setStaffId(cldReplace.getStaffId());
        replaceQuery.setReplaceType(cldReplace.getReplaceType());
        replaceQuery.setReplaceTime(cldReplace.getReplaceTime());
        replaceQuery.setApproveStatus(3L);
        List<CldReplaceVo> isReplaceSize = cldReplaceMapper.selectCldReplaceList(replaceQuery);
        if (isReplaceSize.size() >= cldProjectSet.getMonthCardNumber()) {
            throw new RuntimeException("当月补卡次数已达上限！");
        }
        // 查询当天是否已提交过补卡申请
        replaceQuery = new CldReplaceDto();
        replaceQuery.setProjectId(cldReplace.getProjectId());
        replaceQuery.setStaffId(cldReplace.getStaffId());
        replaceQuery.setReplaceType(cldReplace.getReplaceType());
        replaceQuery.setReplaceTime(cldReplace.getReplaceTime());
        replaceQuery.getParams().put("approveStatus", "0,1");
        isReplaceSize = cldReplaceMapper.selectCldReplaceList(replaceQuery);
        String cldReplaceStr = "";
        if (cldReplace.getReplaceType().equals("1")) {
            cldReplaceStr = "上班卡";
        } else if (cldReplace.getReplaceType().equals("2")) {
            cldReplaceStr = "下班卡";
        } else {
            cldReplaceStr = "上下班卡";
        }
        for (CldReplaceVo cldReplaceVo : isReplaceSize) {
            if (cldReplaceVo.getReplaceType().contains(cldReplace.getReplaceType())) {
                String errorStr = "";
                if (StringUtils.isNotEmpty(staffName)) {
                    errorStr = "[" + staffName + "]当天已申请[" + cldReplaceStr + "]信息,请勿重复提交！";
                } else {
                    errorStr = "当天已申请[" + cldReplaceStr + "]信息,请勿重复提交！";
                }
                throw new RuntimeException(errorStr);
            }
        }
    }

    /**
     * 修改补卡管理
     *
     * @param cldReplace 补卡管理
     * @return 结果
     */
    @Override
    public int updateCldReplace(CldReplace cldReplace) {
        cldReplace.setApproveStatus(1L);
        return cldReplaceMapper.updateCldReplace(cldReplace);
    }

    @Override
    @Transactional
    public int auditCldReplace(CldReplace replace) {
        CldReplace cldReplace = cldReplaceMapper.selectCldReplaceByReplaceId(replace.getReplaceId());
        if (cldReplace == null) {
            throw new RuntimeException("补卡信息查询失败！");
        }
        if (cldReplace.getApproveStatus() == 1) {
            // 查询项目配置 获取当月最多补卡次数
            CldProjectSet cldProjectSet = cldProjectSetMapper.selectCldProjectSetByProjectId(cldReplace.getProjectId());
            // 查询员工当月补卡次数
            CldReplaceDto replaceQuery = new CldReplaceDto();
            replaceQuery.setProjectId(cldReplace.getProjectId());
            replaceQuery.setStaffId(cldReplace.getStaffId());
            replaceQuery.setReplaceType(cldReplace.getReplaceType());
            replaceQuery.setReplaceTime(cldReplace.getReplaceTime());
            replaceQuery.setApproveStatus(3L);
            List<CldReplaceVo> isReplaceSize = cldReplaceMapper.selectCldReplaceList(replaceQuery);
            if (isReplaceSize.size() >= cldProjectSet.getMonthCardNumber()) {
                throw new RuntimeException("当月补卡次数已达上限！");
            }
            // 查询薪酬是否确认
            CldPayConfirm cldPayConfirmQuery = new CldPayConfirm();
            cldPayConfirmQuery.setIsConfirm(1L);
            cldPayConfirmQuery.setStaffId(cldReplace.getStaffId());
            cldPayConfirmQuery.setProjectId(cldReplace.getProjectId());
            cldPayConfirmQuery.setYear(Integer.parseInt(DateUtils.parseDateToStr("yyyy", cldReplace.getReplaceTime())));
            cldPayConfirmQuery.setMonth(Integer.parseInt(DateUtils.parseDateToStr("MM", cldReplace.getReplaceTime())));
            cldPayConfirmQuery.setDay(Integer.parseInt(DateUtils.parseDateToStr("dd", cldReplace.getReplaceTime())));
            List<CldPayConfirm> payConfirmList = cldPayConfirmService.selectCldPayConfirmList(cldPayConfirmQuery);
            if (!payConfirmList.isEmpty()) {
                throw new RuntimeException("当日薪酬已确认,不允许补卡操作！");
            }
            cldReplace.setApproveId(SecurityUtils.getUserId());
            cldReplace.setApproveName(SecurityUtils.getUsername());
            cldReplace.setApproveStatus(replace.getApproveStatus());
            if (cldReplace.getApproveStatus() == 3) {
                // 查询当天考勤记录 修改考勤时间
                CldAttendanceDto attendanceQuery = new CldAttendanceDto();
                attendanceQuery.setProjectId(cldReplace.getProjectId());
                attendanceQuery.setStaffId(cldReplace.getStaffId());
                attendanceQuery.setAttendanceDate(cldReplace.getReplaceTime());
                List<CldAttendanceVo> attendanceList = attendanceService.selectCldAttendanceList(attendanceQuery);
                if (attendanceList.isEmpty()) {
                    throw new RuntimeException("无法查询到当日的考勤数据！");
                }
                CldAttendance cldAttendance = attendanceList.get(0);
                if (cldReplace.getReplaceType().equals("1")) {
                    cldAttendance.setGoToWorkTime1(cldAttendance.getEchoGoToWorkTime1());
                    cldAttendance.setGoToWorkTime2(cldAttendance.getEchoGoToWorkTime2());
                    cldAttendance.setGoToWorkTime3(cldAttendance.getEchoGoToWorkTime3());
                } else if (cldReplace.getReplaceType().equals("2")) {
                    cldAttendance.setGoOffWorkTime1(cldAttendance.getEchoGoOffWorkTime1());
                    cldAttendance.setGoOffWorkTime2(cldAttendance.getEchoGoOffWorkTime2());
                    cldAttendance.setGoOffWorkTime3(cldAttendance.getEchoGoOffWorkTime3());
                } else {
                    cldAttendance.setGoToWorkTime1(cldAttendance.getEchoGoToWorkTime1());
                    cldAttendance.setGoToWorkTime2(cldAttendance.getEchoGoToWorkTime2());
                    cldAttendance.setGoToWorkTime3(cldAttendance.getEchoGoToWorkTime3());
                    cldAttendance.setGoOffWorkTime1(cldAttendance.getEchoGoOffWorkTime1());
                    cldAttendance.setGoOffWorkTime2(cldAttendance.getEchoGoOffWorkTime2());
                    cldAttendance.setGoOffWorkTime3(cldAttendance.getEchoGoOffWorkTime3());
                }
                cldAttendance.setIsReplace(1L);
                attendanceService.updateCldAttendance(cldAttendance);
            } else {
                cldReplace.setResultReason(replace.getResultReason());
            }
        } else {
            throw new RuntimeException("该补卡记录已审核,请勿重复操作！");
        }

        return cldReplaceMapper.updateCldReplace(cldReplace);
    }

    /**
     * 批量删除补卡管理
     *
     * @param replaceIds 需要删除的补卡管理主键
     * @return 结果
     */
    @Override
    public int deleteCldReplaceByReplaceIds(Long[] replaceIds) {
        return cldReplaceMapper.deleteCldReplaceByReplaceIds(replaceIds);
    }

    /**
     * 删除补卡管理信息
     *
     * @param replaceId 补卡管理主键
     * @return 结果
     */
    @Override
    public int deleteCldReplaceByReplaceId(Long replaceId) {
        return cldReplaceMapper.deleteCldReplaceByReplaceId(replaceId);
    }
}
